package com.example.myapplication.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.adp.PhotoUpAdp;
import com.example.myapplication.database.GetItem;
import com.example.myapplication.javaBean.PhotoBean;
import com.example.myapplication.util.ActivityStack;
import com.example.myapplication.util.DbUtils;
import com.example.myapplication.util.PhotoUtil;
import com.example.myapplication.util.TimeUtil;
import com.example.myapplication.util.ToastUtil;

import java.util.ArrayList;

public class PubGetAty extends BaseActivity {
    private TextView tvTitle,btPub;
    private EditText itemTitle,tvTag,etDes;
    private RecyclerView listVIew;
    private PhotoUpAdp photoUpAdp;
    private ArrayList<PhotoBean> data=new ArrayList<>();
    @Override
    protected void logicStart() {

    }

    @Override
    protected void initView() {
        tvTitle=findViewById(R.id.tvTitle);
        btPub=findViewById(R.id.btPub);
        itemTitle=findViewById(R.id.etTitle);
        tvTag=findViewById(R.id.tvTag);
        etDes=findViewById(R.id.etDes);
        listVIew=findViewById(R.id.listView);

        tvTitle.setText("发布需求");

        photoUpAdp=new PhotoUpAdp(data);
        listVIew.setLayoutManager(new GridLayoutManager(this,4));
        listVIew.setAdapter(photoUpAdp);


    }

    @Override
    protected void setListener() {
        super.setListener();
        btPub.setOnClickListener(v -> {
            if (itemTitle.getText().toString().isEmpty()){
                ToastUtil.toast("请填写标题");
                return;
            }
            if (tvTag.getText().toString().isEmpty()){
                ToastUtil.toast("请填写标签");
                return;
            }
            if (etDes.getText().toString().isEmpty()){
                ToastUtil.toast("请填写简介");
                return;
            }
            GetItem getItem = new GetItem();
            getItem.setPubUser(DbUtils.getLoginUser().getId());
            getItem.setDes(etDes.getText().toString());
            getItem.setTitle(itemTitle.getText().toString());
            getItem.setPhotos(PhotoUtil.format(data));
            getItem.setTag(tvTag.getText().toString());
            getItem.setTime(TimeUtil.getTime());
            getItem.save();

            ToastUtil.toast("发布成功");
            ActivityStack.INSTANCE.popOut(PubGetAty.this,true);

        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_pub_get;
    }
}
